package com.cheng.utils;

import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
/*
 *
 *  mailusername=jackchengvips@163.com
   	mailpassword=Ckj2352400400
	mailname=googlecode
	smtphost=smtp.163.com
	port=25*/

public class SendEmailIntenetUtil {

	public static boolean sendEmail(String email, String title, String content){
		// 1.创建一个程序与邮件服务器会话对象 Session
 
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "SMTP");
		props.setProperty("mail.host", "smtp.qq.com");
		props.setProperty("mail.smtp.auth", "true");// 指定验证为true是否需要身份验证
 
		// 创建验证器
		Authenticator auth = new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				// 密码验证 写你们的授权码
				return new PasswordAuthentication("qiuying.qy@foxmail.com", "***********");
				
			}
		};
 
		Session session = Session.getInstance(props, auth);
		// 开启Session的debug模式，这样就可以查看到程序发送Email的运行状态
		// 2.创建一个Message，它相当于是邮件内容
		try {
			Message message = new MimeMessage(session);
 
			message.setFrom(new InternetAddress("qiuying.qy@foxmail.com")); // 设置发送者
 
			message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者
 
			message.setSubject(title);
			// message.setText("这是一封激活邮件，请<a href='#'>点击</a>");
 
			message.setContent(content, "text/html;charset=utf-8");
 
			// 3.创建 Transport用于将邮件发送
 
			Transport.send(message);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} 
	}


}
